import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagementSystem {
    private static ArrayList<Student> studentList = new ArrayList<>();
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            showMenu();
            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    addStudent();
                    break;
                case 2:
                    viewStudents();
                    break;
                case 3:
                    deleteStudent();
                    break;
                case 4:
                    updateStudent();
                    break;
                case 5:
                    System.out.println("退出系统！");
                    return;
                default:
                    System.out.println("无效的选择，请重新输入！");
            }
        }
    }

    private static void showMenu() {
        System.out.println("===== 学生管理系统 =====");
        System.out.println("1. 添加学生");
        System.out.println("2. 查看所有学生");
        System.out.println("3. 删除学生");
        System.out.println("4. 修改学生信息");
        System.out.println("5. 退出系统");
        System.out.print("请输入您的选择：");
    }

    private static void addStudent() {
        System.out.print("请输入学号：");
        String id = scanner.nextLine();

        System.out.print("请输入姓名：");
        String name = scanner.nextLine();

        System.out.print("请输入年龄：");
        int age = scanner.nextInt();
        scanner.nextLine();

        System.out.print("请输入住址：");
        String address = scanner.nextLine();

        Student student = new Student(id, name, age, address);
        studentList.add(student);

        System.out.println("添加学生成功！");
    }

    private static void viewStudents() {
        if (studentList.isEmpty()) {
            System.out.println("当前没有学生信息！");
            return;
        }

        System.out.println("学号\t姓名\t年龄\t住址");

        for (Student student : studentList) {
            System.out.println(student.getId() + "\t" +
                    student.getName() + "\t" +
                    student.getAge() + "岁\t" +
                    student.getAddress());
        }
    }

    private static void deleteStudent() {
        System.out.print("请输入要删除的学生的学号：");
        String id = scanner.nextLine();

        boolean found = false;
        for (int i = 0; i < studentList.size(); i++) {
            if (studentList.get(i).getId().equals(id)) {
                studentList.remove(i);
                found = true;
                System.out.println("学生信息删除成功！");
                break;
            }
        }

        if (!found) {
            System.out.println("未找到该学号的学生！");
        }
    }

    private static void updateStudent() {
        System.out.print("请输入要修改的学生的学号：");
        String id = scanner.nextLine();

        boolean found = false;
        for (Student student : studentList) {
            if (student.getId().equals(id)) {
                System.out.print("请输入新的姓名：");
                student.setName(scanner.nextLine());

                System.out.print("请输入新的年龄：");
                student.setAge(scanner.nextInt());
                scanner.nextLine(); // 清除换行符

                System.out.print("请输入新的住址：");
                student.setAddress(scanner.nextLine());

                found = true;
                System.out.println("学生信息修改成功！");
                break;
            }
        }

        if (!found) {
            System.out.println("未找到该学号的学生！");
        }
    }
}
